package com.hmall.search.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.search.domain.po.ItemDoc;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RequiredArgsConstructor
@Slf4j
public class ItemListener {

    private final ItemClient itemClient;

    private final RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(
            HttpHost.create("http://192.168.150.129:9200")
            )
    );

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.save.queue",durable = "true"),
            exchange =@Exchange(name = "search.direct"),
            key = "items.index"
    ))
    public void indexItemListener (Long id) throws IOException {
        //因为是异步通知，所以在新增完数据后，可以通过新增的数据id查到数据
        ItemDTO itemDTO = itemClient.queryItemById(id);
        if (itemDTO == null){
            return;
        }
        //构建request对象
        IndexRequest request = new IndexRequest("items").id(itemDTO.getId().toString());
        //构建文档对象
        ItemDoc itemDoc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);
        //将文档对象转换成JSON并且存入source中
        request.source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
        //发送新增请求
        client.index(request,RequestOptions.DEFAULT);
        log.info("RabbitMq异步新增es商品信息:{}",itemDoc);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("search.item.update.status.queue"),
            exchange = @Exchange("search.direct"),
            key = "items.update.status"
    ))
    public void updateItemStatusListener(Long id) throws IOException {
        ItemDTO itemDTO = itemClient.queryItemById(id);
        if(itemDTO == null){
            return;
        } else if (itemDTO.getStatus() != 1 ){
            DeleteRequest request = new DeleteRequest("items").id(id.toString());
            client.delete(request,RequestOptions.DEFAULT);
            log.info("将商品状态设置为非正常，已将商品从es中移除，id为：{}",id);
            return;
        }
        ItemDoc doc = BeanUtil.copyProperties(itemDTO, ItemDoc.class);
        IndexRequest request = new IndexRequest("items").id(id.toString());
        request.source(JSONUtil.toJsonStr(doc),XContentType.JSON);
        //发送新增请求
        client.index(request,RequestOptions.DEFAULT);
        log.info("将非正常商品改为正常，RabbitMq异步将新的正常商品新增到es中，商品信息:{}",doc);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("search.item.delete.queue"),
            exchange = @Exchange("search.direct"),
            key = "items.delete"
    ))
    public void  deleteItemListener(Long id) throws IOException {
        DeleteRequest request = new DeleteRequest("items").id(id.toString());
        client.delete(request,RequestOptions.DEFAULT);
        log.info("RabbitMq异步删除商品，商品id:{}",id);
    }


}
